package vip.xiaonuo.facedoor.remote.iservice.adapter;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.context.constant.ConstantContextHolder;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.facedoor.iservice.IFaceDoorDtoService;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;

import java.text.SimpleDateFormat;
import java.util.Date;
import vip.xiaonuo.facedoor.rule.DwRulesEngine;
import vip.xiaonuo.facedoor.vo.dewo.AddPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.DeletePersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonResponse;
import vip.xiaonuo.facedoor.vo.dewo.UpdatePersonRequest;

@Component
public class VisitorReqAdapter {

  public static final String KEY = "ABCD1234";


  @Autowired
  private VisitorService visitorService;

  public ReturnJson deletePerson(VisitorDevice visitorDevice, String uri) throws Exception {
    Visitor byId = visitorService.getById(visitorDevice.getVisitorId());
    DeletePersonRequest deletePersonRequest = new DeletePersonRequest();
    deletePersonRequest.setKey(KEY).setPersonnelId("2"+visitorDevice.getVisitorId().toString()).setMemberOrVisitorDeviceId(visitorDevice.getId()).setMemberType(2);
    deletePersonRequest.setMemberName(byId.getName());
    return DwRulesEngine.faceDoorDtoService.deletePerson(uri, deletePersonRequest);
  }

  public GetPersonResponse getPerson(String uri, Visitor visitor) throws Exception {
    GetPersonRequest getPersonRequest = new GetPersonRequest().setKey(KEY).setQueryType("1").setPersonnelId("2"+visitor.getId().toString());
    return DwRulesEngine.faceDoorDtoService.getPerson(uri, getPersonRequest);
  }

  public ReturnJson addPerson(String uri, Visitor visitor) throws Exception {
    AddPersonRequest addPersonRequest = new AddPersonRequest();
    addPersonRequest.setKey(KEY).setPersonnelId("2"+visitor.getId().toString()).setName(visitor.getName()).setIdCardNum(visitor.getIdCard()).setCompany(visitor.getCompanyName()).setIcNum(visitor.getIcCard()).setMemberOrVisitorDeviceId(visitor.getMemberOrVisitorDeviceId()).setMemberType(visitor.getMemberType()).setWiegandNum(visitor.getWiegandNum()).setPersonnelType("1");
    if (!StringUtils.isEmpty(visitor.getProperValue())) {
      // 如果特征为不为空，那么只传特征，不用传图片，以此减少终端的工作
      addPersonRequest.setFaceFeature(visitor.getProperValue());
    } else {
      addPersonRequest.setFaceFeatureImgPath(visitor.getPhoto());
    }

    addPersonRequest.setExpiryTime(getDate(visitor.getVisitStartDate()) + "," + getDate(visitor.getVisitEndDate()));

    // 湖山数字哨兵 定制
    Boolean hushan_shuzi_shaobing = ConstantContextHolder.getSysConfig("HUSHAN_SHUZI_SHAOBING", Boolean.class, false);
    if (hushan_shuzi_shaobing != null && hushan_shuzi_shaobing&& ObjectUtil.equal(addPersonRequest.getPersonnelType(),"2")) {
      if (StringUtils.isNotBlank(visitor.getName())) {
        addPersonRequest.setName(visitor.getName().charAt(0) +"");
      }
      if (StringUtils.isNotBlank(visitor.getIdCard())){
        addPersonRequest.setIdCardNum(visitor.getIdCard().substring(0,6)+visitor.getIdCard().substring(visitor.getIdCard().length()-4));
      }
    }
    return DwRulesEngine.faceDoorDtoService.addPerson(uri, addPersonRequest);
  }

  public ReturnJson updatePerson(String uri, Visitor visitor) throws Exception {
    UpdatePersonRequest updatePersonRequest = new UpdatePersonRequest();
    updatePersonRequest.setKey(KEY).setPersonnelId("2"+visitor.getId().toString()).setName(visitor.getName())
        .setIdCardNum(visitor.getIdCard()).setCompany(visitor.getCompanyName()).setIcNum(visitor.getIcCard())
        .setMemberOrVisitorDeviceId(visitor.getMemberOrVisitorDeviceId()).setMemberType(visitor.getMemberType()).setWiegandNum(visitor.getWiegandNum()).setPersonnelType("1");
    updatePersonRequest.setFaceFeatureImgPath(visitor.getPhoto());
    updatePersonRequest.setExpiryTime(getDate(visitor.getVisitStartDate()) + "," + getDate(visitor.getVisitEndDate()));
    // 湖山数字哨兵 定制
    Boolean hushan_shuzi_shaobing = ConstantContextHolder.getSysConfig("HUSHAN_SHUZI_SHAOBING", Boolean.class, false);
    if (hushan_shuzi_shaobing != null && hushan_shuzi_shaobing&& ObjectUtil.equal(updatePersonRequest.getPersonnelType(),"2")) {
      if (StringUtils.isNotBlank(visitor.getName())) {
        updatePersonRequest.setName(visitor.getName().charAt(0) +"");
      }
      if (StringUtils.isNotBlank(visitor.getIdCard())){
        updatePersonRequest.setIdCardNum(visitor.getIdCard().substring(0,6)+visitor.getIdCard().substring(visitor.getIdCard().length()-4));
      }
    }
    return DwRulesEngine.faceDoorDtoService.updatePerson(uri, updatePersonRequest);
  }

  public static String getDate(Date date) {
    String nowtime = null;
    try {
      nowtime = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return nowtime;
  }
}
